local p = {}
local getArgs = require('Module:Arguments').getArgs
local inArray = require('Module:Util').inArray
local OrderedTable = require('Module:OrderedTable')

local ELEMENT_MAP = require('Module:Util/Config').ELEMENT_MAP
local RARITY_MAP = require('Module:Util/Config').RARITY_MAP
local SERIES_MAP = require('Module:Util/Config').SERIES_MAP
local split_swap = require('Module:Util/SwapImages').split_swap
local CORE_MAP = require('Module:Util/Core')
local PA_MAP = require('Module:Util/Pa')

-----------------------------------------------------------------------------
-- 生成技能表格头部
-----------------------------------------------------------------------------

function p.header(frame)
	local args = getArgs(frame)
	local html = mw.html.create():tag('div'):addClass('role-skills-box-title color-frame')

    html:tag('div'):addClass('role-skills-box-short'):wikitext('加护技能')
    html:tag('div'):addClass('role-skills-box-long'):wikitext('技能效果')
	
	return html
end

function p.support(frame)
	local args = getArgs(frame)
	local core_id = tonumber(mw.text.split(args.pagename, '/')[2]) or tonumber(args.pagename)
	local core_info = CORE_MAP[''..core_id or ''] or ''
	if (core_info == '') then
		return '【错误】提供的ID有错误'
	end

	local html = mw.html.create()
	
	--第一个技能
	if (core_info.skill1) then
		local skillArgs = core_info.skill1
		skillArgs.count = 0
		p.ability_info(html, skillArgs)
	end

	--第二个技能
	if (core_info.skill2) then
		local skillArgs = core_info.skill2
		skillArgs.count = 1
		p.ability_info(html, skillArgs)
	end

	-- 过一遍解析器
    local frame = mw.getCurrentFrame()
    return frame:preprocess(tostring(html))
end

-----------------------------------------------------------------------------
-- 生成单个技能
-----------------------------------------------------------------------------

function p.ability_info(html, args)
	-- 变量
	local has_upgrade = false  -- 是否有升级技能
	if (args.level[2] or '') ~= '' and (args.level[1] or 0) ~= 0 then
		has_upgrade = true
	end	

	local ability_div
    
	if tonumber(args.count) > 0 then 
		ability_div = html:tag('div'):addClass('role-skills-box-content background-color-2 role-skills-box-top-line')
	else
		ability_div = html:tag('div'):addClass('role-skills-box-content background-color-2')
	end
	local name_div = ability_div:tag('div'):addClass('role-skills-box-short role-skills-box-right-line')

    -- 第一个技能图标
	local skill_icon_div = name_div:tag('div'):addClass('core_icon')
    if (args.level[1] or '') == 0 then
	    p.add_icon(skill_icon_div, args.center, args.param, args.series, args.level[2])
    else
	    p.add_icon(skill_icon_div, args.center, args.param, args.series, args.level[1])
    end

    -- 第一个技能名
	local name_div_block = name_div:tag('div'):addClass('name title-text ec-2-bg-m')
	if (args.level[1] or '') == 0 then
		name_div_block:tag('div'):addClass('text'):tag('span'):addClass('label-item label-item-color-purple learn-lv'):cssText('margin-right: 3px;'):wikitext('满破'):done()
		args.level[1] = args.level[2]
		args.level[2] = ''
	end
    name_div_block:tag('div'):addClass('skill-name'):wikitext(args.name[1]):done()

    -- 如果有第二个技能名/图标
    if has_upgrade then
        name_div:tag('div'):addClass('arrow'):wikitext('▼')

        skill_icon_div = name_div:tag('div'):addClass('core_icon')
        p.add_icon(skill_icon_div, args.element, args.param, args.series, args.level[2])

        -- 第二个技能名
        name_div:tag('div'):addClass('name title-text ec-2-bg-m')
        :tag('div'):addClass('text'):tag('span'):addClass('label-item label-item-color-purple learn-lv'):cssText('margin-right: 3px;'):wikitext('满破'):done()
        :tag('div'):addClass('skill-name'):wikitext(args.name[2]):done()
    end
	
	name_div:done()

	local desc_text = ''

	-- 拼合技能描述
    if (args.before_desc or '') ~= '' then
        desc_text = desc_text..args.before_desc
    end

    if args.use_desc == 'yes' then
        desc_text = desc_text..args.desc
    else
        local auto_desc_array = {}
        -- 如果第一个技能的说明需要被覆盖
        if (args.desc and args.desc[1] or '') ~= '' then
            table.insert(auto_desc_array, args.desc1)
        elseif (args.level[1] or '') ~= '' then
            local temp_desc
            if (args.level[2] or '') ~= '' then
                temp_desc = p.get_skill_desc(args.element, args.param, args.series, args.level[1], true)
            else
                temp_desc = p.get_skill_desc(args.element, args.param, args.series, args.level[1])
            end
            if temp_desc ~= '' then
                table.insert(auto_desc_array, temp_desc)
            end
        end
        if (args.desc and args.desc[2] or '') ~= '' then
            table.insert(auto_desc_array, args.desc2)
        elseif (args.level[2] or '') ~= '' then
            local temp_desc = p.get_skill_desc(args.element, args.param, args.series, args.level[2])
            if temp_desc ~= '' then
                table.insert(auto_desc_array, temp_desc)
            end
        end

        if #auto_desc_array > 0 then
            desc_text = desc_text..table.concat(auto_desc_array, '<br><br>▼<br><br>')
        else
            desc_text = desc_text..args.desc
        end
    end

    if (args.after_desc or '') ~= '' then
        desc_text = desc_text..args.after_desc
    end	
	
	-- 技能效果
	ability_div:tag('div'):addClass('role-skills-box-long')
		:tag('div'):addClass('content'):wikitext(desc_text):done()
	
	return html
end

-----------------------------------------------------------------------------
-- 自动生成图标
-----------------------------------------------------------------------------

function p.add_icon(icon_div, element, param, series, level)
    icon_div:tag('div'):addClass('core-element'):wikitext('[[File:core_element_'..element..'.png]]')
    icon_div:tag('div'):addClass('core-param'):wikitext('[[File:core_param_'..(param % 100000)..'.png]]')
    icon_div:tag('div'):addClass('core-level'):wikitext('[[File:core_level_'..level..'.png]]')
    icon_div:tag('div'):addClass('core-series'):wikitext('[[File:core_series_'..series..'.png]]')

    return icon_div
end

-----------------------------------------------------------------------------
-- 自动生成武器技能说明
-----------------------------------------------------------------------------
 
-- 返回值是技能说明，如果为空字符串表示没有找到技能说明
function p.get_skill_desc(element, param, series, level, noECharts)
	noECharts = true
	local element_name = ELEMENT_MAP[element or ''] or ''
	local series_name = SERIES_MAP[series or ''] or ''
	
	-- 判断是否需要跳过
	local skill_detail_info = PA_MAP[param] or ''

	local no_use = false
	if skill_detail_info then
		if skill_detail_info.no_use then
			no_use = true
		end
	end
 
	if skill_detail_info and not no_use then
		local desc_array = {}
		-- 属性前文字
		if skill_detail_info['before'] then
			table.insert(desc_array, skill_detail_info['before'])
		end
		-- 效果文本
		table.insert(desc_array, skill_detail_info['des'])

		-- 效果量后文字
		if skill_detail_info['after'] then
			table.insert(desc_array, skill_detail_info['after'])
		end
		
		if (skill_detail_info['ECharts'] and (not noECharts)) then
			temp_text = '{{武器浑身曲线|{{浑身数据|'..param..skill_lv ..'-10}}'
			if skill_detail_info[skill_lv][15] then
				temp_text = temp_text..'|{{浑身数据|'..param..skill_lv ..'-15}}'
			end
			if skill_detail_info[skill_lv][20] then
				temp_text = temp_text..'|{{浑身数据|'..param..skill_lv ..'-20}}'
			end
			temp_text = temp_text..'}}'
			table.insert(desc_array, temp_text)
		end

		-- 替换占位符
		local result_str = table.concat(desc_array)
		result_str = mw.ustring.gsub(result_str, '<<ELEMENT>>', element_name)
		result_str = mw.ustring.gsub(result_str, '<<SERIES>>', series_name)
		result_str = mw.ustring.gsub(result_str, '<<ELEMENT>>', element_name)
 
		local detail = skill_detail_info.level[level]
		if detail then
			for i, v in ipairs(detail) do
				local pname = '<<VALUE_'..i..'>>'
				local value = v[1]..'%'
				local after = skill_detail_info.after_detail and skill_detail_info.after_detail[i] or '%'
				if (#v > 1) then
					value = '（{{Label|SLv1|purple}}'..v[1]..after..' / {{Label|SLv4|purple}}'..v[2]..after..'）'
				end
				result_str = mw.ustring.gsub(result_str, pname, value)
			end
		end

		return result_str
	else
		return ''
	end
end

return p
